<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;


use app\models\CrmSystemGsxx;
use app\models\CrmSystemYgxx_ as CrmSystemYgxx;
use app\models\search\CrmSystemYgxxSearch;
use yii\helpers\ArrayHelper;

class BasicInformationSystemYgxxController  extends CommonController
{
    //查询  列表
    public function actionList(){//查询
        $model = new CrmSystemYgxxSearch();
        $post = \Yii::$app->request->post();
        $lists = $model->search($post);
        $limit = is_set($_POST,"limit");
        $offset = is_set($_POST,"limit")*(is_set($_POST,"page")-1);
        $lists->query
            ->limit($limit)
            ->where(['like','dlxl','"'.is_set($_POST,'id').'"'])
            ->offset($offset);
        $lists->query
            ->select('crm_system_ygxx.*,user.username')
            ->leftJoin('user','user.id=crm_system_ygxx.user_id');
        return $this->actionLayData($lists->count,ArrayHelper::toArray($lists->query->all()),0,'',is_set($_POST,"page"));
    }
    //条件查询 员工列表
    public function actionSearchList(){//查询
        $model = new CrmSystemYgxxSearch();
        $post = \Yii::$app->request->post();
        $lists = $model->search($post);
        $limit = is_set($_POST,"limit");
        $offset = is_set($_POST,"limit")*(is_set($_POST,"page")-1);
        $lists->query->limit($limit)->offset($offset);
        $lists->query
            ->select('crm_system_ygxx.*,user.username')
            ->leftJoin('user','user.id=crm_system_ygxx.user_id');
        return $this->actionLayData($lists->count,ArrayHelper::toArray($lists->query->all()),0,'',is_set($_POST,"page"));
    }
    //保存
    public function actionSave(){
        try{
            $product = new CrmSystemYgxx();
            $post = \Yii::$app->request->post();
            $p = $post['CrmSystemYgxx'];
            if (is_set($p,'id')!=""){//修改
                $product = CrmSystemYgxx::findOne($p['id']);
                if($product->load($post,'CrmSystemYgxx')&&$product->update()){
                    return $this->dealFD("修改成功!");
                }else{
                    return $this->dealFD($product->getErrors(),0);
                }
            }else{//新增
                if ($product->load($post,'CrmSystemYgxx') && $product->save()){
                    return $this->dealFD("新增成功!");
                }else{
                    return $this->dealFD($product->getErrors(),0);
                }
            }
        }catch (\Exception $e){
            return $this->dealFD($e,0);
        }
    }
    //删除
    public function actionDelete(){
        $product = CrmSystemYgxxSearch::findOne($_POST['id']);
        if ($product->delete()){
            return $this->dealFD("删除成功!");
        }else{
            return $this->dealFD($product->getErrors(),0);
        }
    }
    //查询所有
    public function actionAllClassify(){
        $classify = new CrmSystemGsxx();
        $list = $classify->find()->select("name as label,id,pid,permission")->asArray()->all();
        return $this->dealFD($this->getElementTree($list,['key'  => 'id','pKey' => 'pid', 'child'=> 'children']));
    }
    //新增分类
    public function actionAddClassify(){
        $classify = new CrmSystemGsxx();
        $parent = $classify->find()->where(['id'=>$_POST['CrmSystemGsxx']['pid']])->one();
        $_POST['CrmSystemGsxx']['permission'] = $parent->permission;
        if ($classify->load($_POST,'CrmSystemGsxx') && $classify->save()){
            return $this->dealFD($classify->attributes);
        }else{
            return $this->dealFD($classify->getErrors(),0);
        }
    }
    //删除 分类
    public function actionDeleteClassify(){
        $classify = CrmSystemGsxx::findOne($_POST['id']);
        if ($classify->delete()){
            $this->deleteAllChildren($_POST['id']);
            return $this->dealFD("删除分类成功!");
        }else{
            return $this->dealFD($classify->getErrors(),0);
        }
    }

    public function deleteAllChildren($id){
        $s = CrmSystemGsxx::findAll(['pid'=>$id]);
        foreach ($s as $k=>$v){
            $v->delete();
            $this->deleteAllChildren($v->id);
        }
        return true;
    }

    public function actionFindNavlist(){
        $yg = CrmSystemYgxx::findOne(['user_id'=>$_POST['userId']]);
        $dlxl = $yg->dlxl[count($yg->dlxl)-1];
        $gs = CrmSystemGsxx::findOne(['id'=>$dlxl]);
        return $this->dealFD($gs==null?null:$gs->permission,1);
    }
}